##
## mssql.py 
##
## Offers mssql functionality for web2Admin script.
## (ie create database, create user)
##
## Author: Shannon Eric Peevey <speeves@erikin.com>, 8 April 2004
##

import os, re, sys
import Mysql

class MssqlFunc:
    """Offers mssql functionality for web2Admin script.
	(ie create database, create user)"""
	
    def AddMssqlDatabase(self, config, utilityObject):
        addRole = ""
        
        x="y"
        while x=="y":
            database = raw_input("What is the name of the database that you would like to create? " )
            user = database + "adm"
            # generate the password for user
            password = self.generatePasswd(config, utilityObject)
            create = "dbcreate"

            command = config['General']["usrBinPath"] + "perl Mssql.pl '" + create + "' '" + config['Mssql']['Server' + config['Session']['Mssql']['CurrentServer']]["mssqlHostname"] + "' '" + config['Mssql']["mssqlUser"] + "' '" + config['Mssql']["mssqlUserPassword"] + "' '" + config['Mssql']["mssqlMaintPlan"] + "' '" + database + "' '" + user + "' '" + password + "' '" + addRole + "'"
            if config['General']["debugLevel"] == '1':
                print command
            os.system(command)

            print "Creating a new Database..."
            x = raw_input("Would you like to create another SQL Server database? (y for yes, n for no) ")

    def AddMssqlUser(self, config, utilityObject):
        x="y"

        if config['General']["debugLevel"] == '5':
            import pdb; pdb.set_trace()

        while x=="y":
            print "\nNext, we need the name of the User(s) for which we would like to add"
            print "database rights. You may enter as many Users as you would like."
            print ""
            print "Press 'q' to finish adding Users\n"

            # set text for prompt
            userName = "User Name? (q => finish) [q] "
            # grab single/multiple Names
            nameList = utilityObject.GetObjectId(config, userName)

            for name in nameList:
                password = self.generatePasswd(config, utilityObject)
                
                database = raw_input("To which database will this user have rights? " )
                print ""
                create = "usercreate"
                
                # let's add this user to the appropriate roles
                y="y"

                while y=="y":
                    print "Now, let's add this user to the appropriate roles."
                    print "Here is a list of appropriate roles for the user: \n"
                    print "(1) db_owner"
                    print "(2) db_datareader"
                    print "(3) db_datawriter"
                    role = raw_input("Please an appropriate role now: [Choose one]")

                    if role == '1':
                        role = 'db_owner'
                    elif role == '2':
                        role = 'db_datareader'
                    elif role == '3':
                        role = 'db_datawriter'
                        
                    command = config['General']["perlPath"] + " Mssql.pl '" + create + "' '" + config['Mssql']['Server' + config['Session']['Mssql']['CurrentServer']]["mssqlHostname"] + "' '" + config['Mssql']["mssqlUser"] + "' '" + config['Mssql']["mssqlUserPassword"] + "' '" + config['Mssql']["mssqlMaintPlan"] + "' '" + database + "' '" + name + "' '" + password + "' '" + role + "'"

                    if config['General']["debugLevel"] == '1':
                        print command

                    os.system(command)

                    # check the mysql.newuser.euid table for existence of user
                    # add them if they are not there
                    checkUser = Mysql.MysqlFunc()
                    checkUser.CheckMysqlNewuserTable(config, name)
                    del checkUser
                        
                    y = raw_input("Would you like to add this user to another role? (y for yes, n for no) ")
                    create = "addrole"

                x = raw_input("Would you like to create another SQL Server database user? (y for yes, n for no) ")


    def generatePasswd(self, config, utilityObject):
        """create password for user
           autogenerate or manual input"""
        autogeneratepw = raw_input("Autogenerate Password for User? (y/n) [y] ")
        if autogeneratepw == "":
            autogeneratepw = 'y'
            
        if autogeneratepw == 'y':
            password = utilityObject.GeneratePassword(config)
        else:
            password = raw_input("User's Password? ")

        return password
